package org.mineground.commands.player;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.DecimalFormat;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.mineground.Main;
import org.mineground.Utilities;

public class Players implements CommandExecutor {
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) throws CommandException {
	Player player = (Player) sender;
	
	if (args.length == 1) {
	    StringBuilder query = new StringBuilder();
	    query.append("SELECT ");
	    query.append("p.last_online, ");
	    query.append("p.registered, ");
	    query.append("t.kills, ");
	    query.append("t.deaths, ");
	    query.append("t.online_time, ");
	    query.append("t.reactiontests, ");
	    query.append("t.blocks_placed, ");
	    query.append("t.blocks_removed ");
	    query.append("FROM lvm_players p ");
	    query.append("LEFT JOIN lvm_player_settings s ON p.player_id = s.player_id ");
	    query.append("LEFT JOIN lvm_player_statistics t ON p.player_id = t.player_id ");
	    query.append("WHERE p.login_name = ?");

	    try {
		PreparedStatement statement = Main.getInstance().getDatabaseHandler().getConnection().prepareStatement(query.toString());
		statement.setString(1, args[0]);

		if (!statement.execute()) {
		    player.sendMessage(args[0] + " is not registered with Las Venturas Mineground.");
		    return true;
		}

		ResultSet result = statement.getResultSet();
		result.next();

		int kills = result.getInt(3);
		int deaths = result.getInt(4);
		int reactionTests = result.getInt(6);
		int blocksPlaced = result.getInt(7);
		int blocksRemoved = result.getInt(8);
		long onlineTime = result.getLong(5);
		Date registered = result.getDate(2);
		Date lastOnline = result.getDate(1);
		DecimalFormat format = new DecimalFormat("#0.00");

		java.util.Date now = new java.util.Date();
		java.util.Date dif = new java.util.Date(registered.getTime());

		player.sendMessage(ChatColor.GREEN + "====================");
		player.sendMessage(ChatColor.YELLOW + "Name: " + args[0]);
		player.sendMessage(ChatColor.YELLOW + "Online time: " + Utilities.formatTime(onlineTime));
		player.sendMessage(ChatColor.YELLOW + "Kills: " + kills);
		player.sendMessage(ChatColor.YELLOW + "Deaths: " + deaths);
		player.sendMessage(ChatColor.YELLOW + "K/D: " + ((deaths > 0) ? (format.format(kills / (double) deaths)) : (format.format((double) kills))));
		player.sendMessage(ChatColor.YELLOW + "Reactiontests won: " + reactionTests);
		player.sendMessage(ChatColor.YELLOW + "Placed blocks: " + blocksPlaced);
		player.sendMessage(ChatColor.YELLOW + "Broken blocks: " + blocksRemoved);
		
		player.sendMessage(ChatColor.YELLOW + "Registered: " + registered.toString() + ((Utilities.daysBetween(dif, now) > 0) ? (" (" + Utilities.daysBetween(dif, now) + " days ago)") : ("")));
		
		dif = new java.util.Date(lastOnline.getTime());
		player.sendMessage(ChatColor.YELLOW + "Last seen: " + lastOnline.toString() + ((Utilities.daysBetween(dif, now) > 0) ? (" (" + Utilities.daysBetween(dif, now) + " days ago)") : ("")));
		player.sendMessage(ChatColor.GREEN + "====================");
	    }

	    catch (Exception e) {
		player.sendMessage(args[0] + " is not registered with Las Venturas Mineground.");
	    }

	    return true;
	}

	String playersOnline;
        StringBuilder playersOnlineBuilder = new StringBuilder();
        
	Player[] onlinePlayers;
	onlinePlayers = Main.getInstance().getServer().getOnlinePlayers();

	for (Player onlinePlayer : onlinePlayers) {
            playersOnlineBuilder.append(onlinePlayer.getName());
            playersOnlineBuilder.append(", ");
	}

	if (onlinePlayers.length == 0) {
	    playersOnline = ChatColor.DARK_AQUA + "Online Players: " + ChatColor.AQUA + "Nobody is Minecrafting right now.";
        }
        
        else {
	    playersOnline = ChatColor.DARK_AQUA + "Online Players " + ChatColor.AQUA + "(" + onlinePlayers.length + "): " + playersOnlineBuilder.toString().substring(0, playersOnlineBuilder.toString().length() - 2);
        }
        
	player.sendMessage(playersOnline);
	return true;
    }
}
